//==================================================================
// Created by Sun Jiang
// Email: sunjiang@paracs.com,
//        sunjiang1993@126.com
// Date: 2025-01-10, 11:40:25
// Copyright (c) 2021-2025 YUHAI.  All Right Reserved!
//===================================================================

#include "blas.h"

void vector_creat(std::vector<double>& x, int N, double x0)
{
    /// 创建长度为N，初始值为x0的数组
    x.resize(N, x0);
}

void vector_creat_random(std::vector<double>& x, int N, double xmin, double xmax)
{
    /// 创建长度为N，在[xmin, xmax]范围中随机分布的数组
    // 创建一个随机设备对象用于种子
    std::random_device rd;

    // 创建一个 Mersenne Twister 引擎并用随机设备初始化
    std::mt19937 gen(rd());

    // 定义均匀分布，指定范围 [min, max]
    std::uniform_real_distribution<double> dis(xmin, xmax);

    x.resize(N, 0.0);
    // 生成随机数
#pragma omp parallel for
    for (int i = 0; i < N; i++) {
        x[i] = dis(gen);
    }

}

double dot(const std::vector<double>& x, const std::vector<double>& y)
{
   /// @brief 计算向量点积：dot = x[0]*y[0] + x[1]*y[1] + ... + x[n]*y[n]
   double sum = 0.0;
   std::size_t N = x.size();

#pragma omp parallel for reduction(+:sum)
   for (int i = 0; i < N; i++) {
       sum += x[i] * y[i];
       //std::cout << " i = " << i << "\n";
   }

   return sum;
}